﻿using RentACarRESTApi.Models;
using RentACarRESTApi.Services;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace RentACarRESTApi.Controllers
{
    public class ReservationsController : ApiController
    {
        private ReservationsRepository reservationsRepository;

        public ReservationsController()
        {
            reservationsRepository = new ReservationsRepository();
        }

        public IEnumerable<Reservation> Get()
        {
            return reservationsRepository.GetAllReservations();
        }

        public Reservation Get(int id)
        {
            return reservationsRepository.GetReservation(id);
        }

        public HttpResponseMessage Post([FromUri]int carId, [FromUri]DateTime startDate, [FromUri]DateTime endDate)
        {
            HttpResponseMessage response = null;
            Reservation reservation = new Reservation(idCarReserved: carId, startDate: startDate, endDate: endDate);

            bool resp = reservationsRepository.SaveReservation(reservation);

            if (resp == true)
            {
                response = Request.CreateResponse<Reservation>(System.Net.HttpStatusCode.Created, reservation);
            }
            else
            {
                response = Request.CreateResponse<Reservation>(System.Net.HttpStatusCode.Conflict, reservation);
            }

            return response;
        }

        public HttpResponseMessage Post(Reservation reservation)
        {
            bool resp = reservationsRepository.SaveReservation(reservation);
            HttpResponseMessage response = null;

            if (resp == true)
            {
                response = Request.CreateResponse<Reservation>(System.Net.HttpStatusCode.Created, reservation);
            }
            else
            {
                response = Request.CreateResponse<Reservation>(System.Net.HttpStatusCode.Conflict, reservation);
            }

            return response;
        }

        public HttpResponseMessage Put(int id, Reservation reservation)
        {
            HttpResponseMessage response = null;

            if (reservationsRepository.ContainsReservation(id))
            {
                bool res = reservationsRepository.UpdateReservation(reservation);
                if (res)
                {
                    response = Request.CreateResponse<Reservation>(HttpStatusCode.OK, reservation);
                }
                else
                {
                    response = Request.CreateResponse<Reservation>(HttpStatusCode.Conflict, reservation);
                }
            }
            else
            {
                response = Request.CreateResponse<Reservation>(HttpStatusCode.NotFound, reservation);
            }

            return response;
        }

        public HttpResponseMessage Delete(int id)
        {
            HttpResponseMessage response = null;

            if (reservationsRepository.ContainsReservation(id))
            {
                bool res = reservationsRepository.DeleteReservation(id);
                if (res)
                {
                    response = Request.CreateResponse(HttpStatusCode.OK);
                }
                else
                {
                    response = Request.CreateResponse(HttpStatusCode.ExpectationFailed);
                }
            }
            else
            {
                response = Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return response;
        }
    }
}
